package com.etpjava.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.etpjava.dao.SalaryDao;
import com.etpjava.model.Employee;
import com.etpjava.model.Pay;
import com.etpjava.util.GetSth;
import com.etpjava.util.Page;

public class SalaryDaoImpl implements SalaryDao {
	static Log logger = LogFactory.getLog(SalaryDaoImpl.class);

	public Session getSession() {
		Configuration cfg = new Configuration();
		SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
		return sessionFactory.openSession();
	}

	@Override
	public void findAll(Page p) {
		Session session = this.getSession();
		session.beginTransaction();
		int rowcount = ((Long) session.createQuery("select count(id) from Pay")
				.list().get(0)).intValue();
		logger.info(rowcount);
		p.setRowTotalCount(rowcount);
		String sql = "select p from Pay p";
		Query query = session.createQuery(sql)
				.setFirstResult(p.getRowStartCount())
				.setMaxResults(Page.pagesize);
		p.setList(query.list());
		session.close();

	}

	@Override
	public void delete(Pay sa) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			session.delete(sa);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
		} finally {
			session.close();
		}
	}

	@Override
	public boolean save(Pay sa) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			session.save(sa);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public void update(Pay sa) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			session.update(sa);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
		} finally {
			session.close();
		}

	}

	@Override
	public Pay findById(int id) {
		Session session = this.getSession();
		session.beginTransaction();
		Pay pay = (Pay) session.get(Pay.class, id);
		session.getTransaction().commit();

		return pay;
	}

	

	@Override
	public void searchByEmId(Page p, int id) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			String sql="from Pay p where p.myeEmployee.id=? ";
			Query query=session.createQuery(sql);
			query.setParameter(0, id);
			p.setRowTotalCount(1);
			p.setList(query.list());
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
		} finally {
			session.close();
		}

	}
	@Override
	public void searchByEmName(Page p, String name) {
		Session session = this.getSession();
		session.beginTransaction();
		try {
			String sql="from Pay p where p.myeEmployee.name=? ";
			String sql2="select count(p.id) from Pay p where p.myeEmployee.name=? ";
			Query query2=session.createQuery(sql2);
			query2.setParameter(0, name);
			int count=((Long)query2.list().get(0)).intValue();
			p.setRowTotalCount(count);
			Query query=session.createQuery(sql);
			query.setParameter(0, name);
			query.setFirstResult(p.getRowStartCount());
			query.setMaxResults(Page.pagesize);
			p.setList(query.list());
			session.getTransaction().commit();
		} catch (HibernateException e) {
			session.getTransaction().rollback();
		} finally {
			session.close();
		}

	}
	@Override
	public void search(Page p, Map<String, String> sw) {
		Session session = this.getSession();
		session.beginTransaction();
		StringBuffer sws = new StringBuffer();
		if (sw.containsKey("minid")) {
			sws.append(" id  >= " + sw.get("minid") + " and ");
		}
		if (sw.containsKey("maxid")) {
			sws.append(" id  <= " + sw.get("maxid") + " and ");
		}

		if (sw.containsKey("sname")) {
			sws.append(" p.myeEmployee.name like " + "'%" + sw.get("sname") + "%'" + " and ");
		}
		
		if (sw.containsKey("sminbase")) {
			sws.append(" basePay >= " + sw.get("sminbase") + " and ");
		}
		if (sw.containsKey("smaxbase")) {
			sws.append(" basePay <= " + sw.get("smaxbase") + " and ");
		}

		
		if (sw.containsKey("sminage")) {
			//sws.append(" workAge >= " +  sw.get("sminage") + " and ");
			sws.append(" p.myeEmployee.firstWorkTime <= " + "'"
					+ GetSth.getDate(Integer.parseInt(sw.get("sminage"))) + "'"
					+ " and ");
		}
		if (sw.containsKey("smaxage")) {
			//sws.append(" workAge <= " + sw.get("smaxage") + " and ");
			sws.append(" p.myeEmployee.firstWorkTime >= " + "'"
					+ GetSth.getDate(Integer.parseInt(sw.get("smaxage"))) + "'"
					+ " and ");
		}

		if (sw.containsKey("sminpt")) {
			sws.append(" time >= " + "'"
					+ GetSth.getDate(Integer.parseInt(sw.get("sminpt"))) + "'"
					+ " and ");
		}
		if (sw.containsKey("smaxpt")) {
			sws.append(" time <= " + "'"
					+ GetSth.getDate(Integer.parseInt(sw.get("smaxpt"))) + "'"
					+ " and ");
		}
		String sql = null;
		String sql2 = null;
		if (sws.toString().indexOf("and") == -1) {
			sql = "from Pay";
			sql2 = "select count(p.id) from Pay p";
		} else {
			int kk = sws.lastIndexOf("and");
			sql = "from Pay p where" + sws.subSequence(0, kk);
			sql2 = "select count(p.id) from Pay p where"
					+ sws.subSequence(0, kk);
		}
		logger.info(sql);
		Query query = session.createQuery(sql2);
		int rowc = ((Long) query.list().get(0)).intValue();
		p.setRowTotalCount(rowc);
		Query query2 = session.createQuery(sql);
		query2.setFirstResult(p.getRowStartCount());
		query2.setMaxResults(p.pagesize);
		session.getTransaction().commit();
		@SuppressWarnings("unchecked")
		List<Pay> ems = (List<Pay>) query2.list();
		p.setList(ems);
		session.close();
	}

}
